package org.apache.ibatis.application;

import org.apache.ibatis.application.mapper.BlogMapper;
import org.apache.ibatis.application.model.po.Blog;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Programs {

  public static void main(String[] args) throws IOException {
    // https://mybatis.org/mybatis-3/zh/getting-started.html
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 获取数据库源，XMLConfigBuilder.parse()，构建Configuration对象
    // SqlSessionFactory 派生了 DefaultSqlSessionFactory ， DefaultSqlSessionFactory的构造函数传入Configuration对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //
    try (SqlSession session = sqlSessionFactory.openSession()) {
      // mybatis-config.xml <mapper resource="mapper/BlogMapper.xml"/>
      // Blog blog = session.selectOne("org.apache.ibatis.application.mapper.BlogMapper.selectBlog", 1001);

      // mybatis-config.xml <mapper class="org.apache.ibatis.application.mapper.BlogMapper"/>
      BlogMapper mapper = session.getMapper(BlogMapper.class);
      Blog blog = mapper.selectBlog(1001);
      System.out.println(blog);
    }
  }
}
